package com.alm.token.ui.viewpagefragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.ui.activity.ActivityListActivity;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.BuyEggPartnerActivity;
import com.alm.token.ui.activity.CandyActivity;
import com.alm.token.ui.activity.CrowdfundingActivity;
import com.alm.token.ui.activity.InviteFriendActivity;
import com.alm.token.ui.activity.MessageActivity;
import com.alm.token.ui.activity.QuantitativeOrderActivity;
import com.alm.token.ui.activity.SuperColorEggActivity;
import com.alm.token.ui.activity.TransformActivity;
import com.alm.token.ui.activity.TurnOutBiTBActivity;
import com.alm.token.ui.activity.TurnOutBiTB_FukuanActivity;
import com.alm.token.ui.data.bean.CorwdIndexData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.CrowdFundView;
import com.alm.token.utils.Utils;

import static com.alm.token.ConStant.ISDEBUG;

public class ApplicationPageFrag extends BaseViewPageFragment implements CrowdFundView<CorwdIndexData> {

    @BindView(R.id.message)
    ImageView message;
    @BindView(R.id.module1)
    RelativeLayout module1;
    @BindView(R.id.module2)
    RelativeLayout module2;
    @BindView(R.id.module3)
    RelativeLayout module3;
    @BindView(R.id.module4)
    RelativeLayout module4;
    @BindView(R.id.module5)
    RelativeLayout module5;
    @BindView(R.id.module6)
    RelativeLayout module6;
    @BindView(R.id.module7)
    RelativeLayout module7;
    @BindView(R.id.module8)
    RelativeLayout module8;
    @BindView(R.id.module9)
    RelativeLayout module9;
    @BindView(R.id.module10)
    RelativeLayout module10;
    @BindView(R.id.invite_friend)
    TextView inviteFriend;
    private CorwdIndexData corwdIndexData;

    public static ApplicationPageFrag newInstance() {
        ApplicationPageFrag fragment = new ApplicationPageFrag();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.viewpage_fragment_application;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(View view) {

        int itemWidth = getActivity().getResources().getDisplayMetrics().widthPixels;
        int imageWidth = (itemWidth - Utils.dp2px(getContext(), 30)) / 3;

        setModuleView(module3, imageWidth, 0, 0);
        setModuleView(module2, imageWidth, 5, 5);
        setModuleView(module4, imageWidth, 0, 0);

        setModuleView(module5, imageWidth, 0, 0);
        setModuleView(module6, imageWidth, 5, 5);
        setModuleView(module9, imageWidth, 0, 0);

        setModuleView(module10, imageWidth, 0, 0);
        setModuleView(module7, imageWidth, 5, 5);
        setModuleView(module8, imageWidth, 0, 0);

        inviteFriend.setOnClickListener(this);
        message.setOnClickListener(this);
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.module1:
                preenter.getCorwdIndexData();
//                startActivity(CrowdfundingActivity.class);
                break;
            case R.id.module2:
//                if (MyApp.getCurrencyChangeIndexDataBean() != null) {
//                    if (MyApp.getCurrencyChangeIndexDataBean().getWallets() != null &&
//                            MyApp.getCurrencyChangeIndexDataBean().getWallets().size() != 0) {
//                        startActivity(TransformActivity.class);
//                    }else {
//                        showToast("当前未开启转换,请稍候尝试!");
//                    }
//                } else {
//                    preenter.getCurrencyChangeIndexData();
//                    showToast("请稍后重试");
//                }
                startActivity(TransformActivity.class);
                break;
            case R.id.module3:
//                if (ConStant.ISDEBUG) {
                startActivity(QuantitativeOrderActivity.class);
//                } else {
//                    showToast(getString(R.string.common_opentip1));
//                }
                break;

            case R.id.module4://币互转
//                if(ConStant.ISDEBUG){
//                    startActivity(TurnOutBiTB_FukuanActivity.class);
                startActivity(TurnOutBiTBActivity.class);
//                }else {
//                    showToast(getString(R.string.common_opentip1));
//                }
//                startActivity(SuperColorEggActivity.class);
                break;
            case R.id.module5://糖果
//                if(ConStant.ISDEBUG){
                startActivity(CandyActivity.class);
//                }else {
//                    showToast(getString(R.string.common_opentip1));
//                }


//                startActivity(SuperColorEggActivity.class);//超级彩蛋页面
                break;
            case R.id.module6:
//                if(ISDEBUG){
                startActivity(SuperColorEggActivity.class);
//                }else {
//                    showToast(getString(R.string.common_opentip1));
//                }
                break;
            case R.id.module7:
//                showToast("更多功能开发中...");
                showToast(getString(R.string.common_moreapp));
                break;
            case R.id.module8:
//                startActivity(BuyEggPartnerActivity.class);
                break;
            case R.id.module9:
//                if(ISDEBUG){
                startActivity(ActivityListActivity.class);
//                }else {
//                    showToast(getString(R.string.common_opentip1));
//                }
                break;
            case R.id.module10:
//                showToast("更多功能开发中...");
                showToast(getString(R.string.common_opentip1));
                break;
            case R.id.invite_friend:
                startActivity(InviteFriendActivity.class);
                break;
            case R.id.message:
                startActivity(MessageActivity.class);
                break;
        }
    }

    public void setModuleView(RelativeLayout moduleView, int width, int left, int right) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width);
        params.setMargins(Utils.dp2px(getContext(), left), 0, Utils.dp2px(getContext(), right), 0);
        moduleView.setLayoutParams(params);
        moduleView.setOnClickListener(this);
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(), PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void getData(CorwdIndexData corwdIndexData) {
        if (corwdIndexData.getStatus() == 0 && corwdIndexData.getData() != null) {
            if (corwdIndexData.getData().getCfd() == null) {
//                showToast("众筹活动暂未开启,请稍候尝试");
                showToast(getString(R.string.app_hint_crowdfundwait));
                return;
            }
            Intent intent = new Intent(getContext(), CrowdfundingActivity.class);
            intent.putExtra("CorwdIndexDataBean", corwdIndexData);
            startActivity(intent);
        }
        if (corwdIndexData.getData() == null) {
//            showToast("众筹活动暂未开启,请稍候尝试");
            showToast(getString(R.string.app_hint_crowdfundwait));
        }

    }

    @Override
    public void onError(String error) {

    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }
}
